package com.example.myapplication.activity

import android.content.Context
import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.example.myapplication.R
import com.example.myapplication.databinding.ActivityInternetBinding

class InternetActivity : AppCompatActivity() {
    lateinit var binding: ActivityInternetBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        binding = ActivityInternetBinding.inflate(layoutInflater)
        setContentView(binding.root)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }

        val sharedPreferences =
            binding.root.context.getSharedPreferences("ipAddress", Context.MODE_PRIVATE)
        val editor = sharedPreferences.edit()
        val ip = sharedPreferences.getString("ip", null)
        val port = sharedPreferences.getString("port", null)
        if (ip != null) {
            binding.ipEditText.setText(ip)
            binding.portEditText.setText(port)
        }
        binding.button.setOnClickListener {
            val ipText = binding.ipEditText.text
            val portText = binding.portEditText.text
            editor.putString("ip", ipText.toString())
            editor.putString("port", portText.toString())
            editor.commit()
            finish()
        }
    }
}